package com.csh.szhm.http;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.csh.szhm.utils.StringUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class GsonRequest<T> extends JsonRequest<T> {
	private final Gson mGson = new Gson();
	private final Listener<T> mListener;
	private TypeToken<T> mTypeToken;
	private Class<?> cls;

	public GsonRequest(int method, String url, Listener<T> listener,
			ErrorListener errorListener) {
		// super(method,null,null, url, errorListener);
		super(method, url, null, listener, errorListener);
		this.mListener = listener;
		setShouldCache(true);
		System.out.println("url->>>" + url);
	}

	public void setTypeToke(TypeToken<T> t) {
		mTypeToken = t;
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		Map<String, String> headers = new HashMap<String, String>();
		// 添加头
		headers.put("Charset", "UTF-8");
//		headers.put("User-Agent", "Secretary/" + AppConfig.getVersionName()
//				+ " Android/" + android.os.Build.VERSION.RELEASE);
//		String toke = SharedPreferencesUtil.getInstance().getToken();
//		if (!StringUtil.isEmptyString(toke)) {
//			headers.put("Authorization", "Token " + toke);
//		}
//		headers.put("IMEI", AppConfig.getImei());
//		headers.put("Location", CollegeApplication.mMyLocation);
		return headers;
	}

	@Override
	protected Response<T> parseNetworkResponse(NetworkResponse response) {
		try {
			String json = new String(response.data,
					HttpHeaderParser.parseCharset(response.headers));
			T temp = null;
			if (mTypeToken != null) {
				temp = mGson.fromJson(json, mTypeToken.getType());
			}
			return Response.success(temp,
					HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		} catch (JsonSyntaxException e) {
			return Response.error(new ParseError(e));
		}
	}

	@Override
	protected void deliverResponse(T response) {
		mListener.onResponse(response);
	}

	private String requestBody = "";

	/**
	 * 设置post请求数据
	 * 
	 * @param <E>
	 * 
	 * @param t
	 */
	public <E> void setRequestBody(E t) {
		requestBody = mGson.toJson(t);
		System.out.println("---json>>>"+requestBody);
	}

	public void setRequestString(String st) {
		if (!StringUtil.isEmptyString(st)) {
			requestBody = st;
		}
	}

	@Override
	public byte[] getBody() {

		try {
			return requestBody.getBytes(getParamsEncoding());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

}
